#ifndef DISPLAY_H
#define DISPLAY_H
#include <ibus.h>
#include <stdio.h>
#include "immessage.h"

#define FW(x) ((x) * info.fontWidth)
#define FH(y) ((y) * info.fontHeight)
#define SW (info.screenWidth)
#define SH (info.screenHeight)

#define MARGIN 5
#define GAP 5
#define WIN_HEIGHT (FH(1) + 2 * MARGIN)
#define WIN_INTERVAL (WIN_HEIGHT + GAP)

#define COLOR_BG Gray
#define COLOR_FG Black
#define COLOR_ACTIVE_CANDIDATE DarkBlue

//#define debug(args...) fprintf(stderr, args)
#define debug(args...)

enum { AuxiliaryTextWin = 0, PreeditTextWin, LookupTableWin, StatusBarWin };
IBusText *auxiliary_text;
IBusText *preedit_text;
IBusLookupTable *lookup_table;
IBusPropList *property_list;
IBusInputContext *ibus_ctx;
unsigned short cursor_x, cursor_y;
Rectangle auxiliary_text_win, preedit_text_win, lookup_table_win, status_bar_win;
Info info;

struct interval {
	unsigned first;
	unsigned last;
};

struct _IBusPropertyPrivate {
    gchar    *key;
    gchar    *icon;
    IBusText *label;
    IBusText *tooltip;

    gboolean sensitive;
    gboolean visible;
    IBusPropType type;
    IBusPropState state;

    IBusPropList *sub_props;
};

struct _IBusEngineDescPrivate {
    gchar      *name;
    gchar      *longname;
    gchar      *description;
    gchar      *language;
    gchar      *license;
    gchar      *author;
    gchar      *icon;
    gchar      *layout;
    guint       rank;
    gchar      *hotkeys;
    gchar      *symbol;
    gchar      *setup;
};

char bisearch(unsigned ucs, const struct interval *table, unsigned max);
char is_double_width(unsigned ucs);
inline unsigned short get_cursor_y();
void utf8_to_utf16(const unsigned char *utf8, unsigned short *utf16);
unsigned text_width(const char *utf8);
void draw_margin(Rectangle rect, char color);
void calculate_lookup_win();
void draw_lookup_table();
void calculate_preedit_win();
void calculate_auxiliary_win();
void draw_auxiliary_text();
void draw_preedit_text();
void calculate_status_win();
void draw_status_bar();
#endif
